package com.mic.yzzn.plc.core.ins.toolUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

/**
 * Created by ASH on 16/7/17.
 */
public class ArrayQueue<E> implements Queue<E> {

    private ArrayList<E> array;

    public ArrayQueue() {
        array = new ArrayList<>();
    }

    @Override
    public int size() {
        return array.size();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return array.contains(o);
    }

    @Override
    public Iterator<E> iterator() {
        return array.iterator();
    }

    @Override
    public Object[] toArray() {
        return array.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return array.toArray(a);
    }

    @Override
    public boolean add(E e) {
        return array.add(e);
    }

    @Override
    public boolean remove(Object o) {
        return array.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return array.containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return array.addAll(c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return array.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return array.retainAll(c);
    }

    @Override
    public void clear() {
        array.clear();
    }

    @Override
    public boolean offer(E e) {
        array.add(e);
        return true;
    }

    @Override
    public E remove() {
        E o = array.get(0);
        array.remove(o);
        return o;
    }

    @Override
    public E poll() {
        if (array.isEmpty()) {
            return null;
        } else {
            E o = array.get(0);
            array.remove(o);
            return o;
        }
    }

    @Override
    public E element() {
        return array.get(0);
    }

    @Override
    public E peek() {
        if (array.isEmpty()) {
            return null;
        } else {
            return array.get(0);
        }
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> queue = new ArrayQueue<>();
        for (int i = 0; i < 10; ++i) {
            queue.add(i + 1);
        }

        for (int i = 0; i < 10; ++i) {
            System.out.print(queue.remove() + " ");
        }
    }
}
